#pragma once
#include "CRank.h"
extern void errorMessage(WCHAR* str);

CRank::CRank():fx(100),fy(100),fh(20)
{
	myFont = al_load_ttf_font("C:\\WINDOWS\\Fonts\\MSYH.TTF", 20, 0);
	if (!myFont)
	{
		errorMessage(L"can't open MSYH.ttf");
	}
	readRank();
}
void CRank::draw()
{
	float x = fx;
	float y = fy;
	for (vector<string>::iterator itor = ranker.begin(); itor != ranker.end(); itor++)
	{
		al_draw_text(myFont, al_map_rgb(0,0,0), x, y, 0, itor->c_str());
		y += fh;
	}
}
void CRank::addNewRank(string str)
{
	for (vector<string>::iterator itor = ranker.begin(); itor != ranker.end(); itor++)
	{
		if(itor->compare( 14, 10, str, 14, 10) < 0)
		{
			ranker.insert(itor, str);
			break;
		}
	}
}

CRank::~CRank()
{
	al_destroy_font(myFont);
	ofstream outfile(filename.c_str(), ofstream::out| ofstream::trunc);
	for (int i = 0; i < 10; i++)
	{
		outfile << ranker[i] << endl;
	}
	ranker.clear();
}

void CRank::readRank()
{
	filename = "rank.txt";
	ifstream infile;
	infile.open(filename.c_str());
	if (!infile)
	{
		errorMessage(L"can't open the rank.txt");
	}
	while (infile >> temp)
	{
		ranker.push_back(temp);
	}
	infile.close();
	infile.clear();

}